/**
 * 
 */
package com.shiplanet.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.shiplanet.entity.Category;
import com.shiplanet.entity.Post;
import com.shiplanet.service.CategoryService;
import com.shiplanet.service.PostService;
import com.shiplanet.util.Constants;

/**
 * @author joesong
 *
 */
@Controller
public class PostAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	@Resource
	private PostService postService;

	@Resource
	private CategoryService categoryService;

	// parameters for save();
	private Post post;

	private Integer categoryId;

	private File[] photoFiles;
	private File[] file;
	private String[] photoContentType;
	private String[] photoFileName;

	public String save() throws Exception {
		// present starting page
		if (post == null) {
			return "savepostpage";
		}

		// initiate post
		post.setApproved(true);
		post.setCreateTime(new Date());
		System.out.println("isFeatured: " + post.isFeatured());

		// initiate category
		if (categoryId != null) {
			Category cat = categoryService.getCategory(categoryId);
			post.setCategory(cat);
		}

		// initiate photos
		String realRootPath = ServletActionContext.getServletContext()
				.getRealPath("/");

		if (postService.savePostWithPhoto(post, photoFiles, realRootPath) != null) {
			post = null;
			categoryId = null;
			return "savepostpage";
		} else {
			return ERROR;
		}
	}

	public String saveMain() {
		if (categoryId != null) {
			Category cat = categoryService.getCategory(categoryId);
			post.setCategory(cat);

			// initiate post
			post.setCreateTime(new Date());

			if (postService.savePost(post) != null) {
				return "listitempage2";
			} else {
				return ERROR;
			}
		}

		return "listitempage1";
	}

	public String saveAttach() throws Exception {
		// initiate photos
		if (postId != null) {
			String realRootPath = ServletActionContext.getServletContext()
					.getRealPath("/");
			
			Post post = postService.getPost(postId);
			
			postService.addPostPhotoFiles(post, file, realRootPath);
		}
		return "listitempage2";
	}

	// parameters for list();
	// current page in the navigation list
	private int currentPage = 1;
	private int lastPage = 1;
	private List<Post> postList;

	public String list() {
		final int rows = Constants.PAGINATION_ROWS;
		long postCount;

		postCount = postService.countPost();
		if (postCount > 0)
			lastPage = (int) (postCount % rows == 0 ? postCount / rows
					: postCount / rows + 1);

		postList = postService.findPost(currentPage, rows);
		return "listpostpage";
	}

	// parameters for post();
	private Integer postId;

	public String update() throws Exception{
		if (postId == null) {
			return "updatepostpage";
		}

		if (postId != null && post == null) {
			post = postService.getPost(postId);
			return "updatepostpage";
		}

		if (postId != null && post != null) {
			Post originalPost;
			String realRootPath = ServletActionContext.getServletContext()
					.getRealPath("/");
			
			originalPost = postService.getPost(postId);

			postService.updatePostWithPhoto(originalPost, post, photoFiles, realRootPath);
			post = null;
			
			return "updatepostpage";
		}

		return "updatepostpage";
	}

	public String delete() {
		if (postId != null) {
			String realRootPath = ServletActionContext.getServletContext()
					.getRealPath("/");
			
			Post post = postService.getPost(postId);
			postService.deletePost(post, realRootPath);
		}
		return "listPostAction";
	}

	public String view() {
		if (postId != null) {
			post = postService.getPost(postId);
		}

		// replace \r\n with <br/> to display properly on front end.
		String equipmentDesc = post.getEquipmentDescription();
		post.setEquipmentDescription(equipmentDesc.replace("\r\n", "<br/>"));

		return "postpage";
	}

	public Integer getPostId() {
		return postId;
	}

	public void setPostId(Integer postId) {
		this.postId = postId;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public File[] getPhotoFiles() {
		return photoFiles;
	}

	public String[] getPhotoContentType() {
		return photoContentType;
	}

	public String[] getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFiles(File[] photoFiles) {
		this.photoFiles = photoFiles;
	}

	public void setPhotoContentType(String[] photoContentType) {
		this.photoContentType = photoContentType;
	}

	public void setPhotoFileName(String[] photoFileName) {
		this.photoFileName = photoFileName;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public File[] getFile() {
		return file;
	}

	public void setFile(File[] file) {
		this.file = file;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public List<Post> getPostList() {
		return postList;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}

	public void setPostList(List<Post> postList) {
		this.postList = postList;
	}

}
